package work;


public class Customer extends Thread {
    Baozi baozi;
    BaoziBox baoziBox;

    public Customer(Baozi baozi, BaoziBox baoziBox) {
        this.baozi = baozi;
        this.baoziBox = baoziBox;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (baozi) {
                if (!baoziBox.flag) {
                    try {
                        baozi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                if (baoziBox.getCount() == 5) {
                    while (baoziBox.getCount() != 0) {
                        System.out.println(Thread.currentThread().getName() + "消费者正在消费：" + baoziBox.fetch().toString());
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("消费完成，等待生产者生产");
                }
                if (baoziBox.getCount() == 0) {
                    //System.out.println("消费完成，等待生产者生产包子");
                    baoziBox.flag = false;
                    baozi.notifyAll();
                }
            }
        }
    }

}
